package com.appistry.maven.plugin.far.remote.ssh;

import com.appistry.maven.plugin.far.util.Assert;

/**
 * Factory class that returns the appropriate <code>Command</code> object
 * 
 */
public class CommandObjectFactory {

	public enum COMMAND_TYPE {
		ssh, sftp
	};

	/**
	 * Instantiates and returns a <code>Command</code> object
	 * 
	 * @param type
	 *            either ssh or sftp
	 * @return Command object or null
	 * @throws RuntimeException
	 *             if invalid type is passed
	 */
	public static Command getInstance(String type) {
		Assert.notNull(type);

		if (COMMAND_TYPE.sftp.name().equalsIgnoreCase(type)) {
			return new SftpCommand();
		} else if (COMMAND_TYPE.ssh.name().equalsIgnoreCase(type)) {
			return new SshCommand();
		} else {
			throw new RuntimeException(
					"Valid types are ssh and sftp. Could not create Command object for type: "
							+ type);
		}

	}

	/**
	 * Instantiates and returns a <code>Command</code> object. The
	 * <code>Command</code> object that is instantiated is based on the instance
	 * of type <code>RemoteOperation</code> that is passed as an argument i.e.
	 * either <code>Ssh</code> or <code>Sftp</code>
	 * 
	 * @param ro
	 *            instance of <code>RemoteOperation</code>. In this case either
	 *            <code>Ssh</code> or <code>Sftp</code>
	 * @return Command object
	 * @throws RuntimeException
	 *             if invalid <code>RemoteOperation</code> is thrown
	 */
	public static Command getInstance(RemoteOperation ro) {
		Assert.notNull(ro);

		if (ro instanceof Sftp) {
			return new SftpCommand(ro);
		} else if (ro instanceof Ssh) {
			return new SshCommand(ro);
		} else {
			throw new RuntimeException(
					"Valid RemoteOperation types are Ssh and Sftp. Could not create Command object for type: "
							+ ro.getClass().getName());
		}

	}

}
